package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.constant.WorkConstant;
import com.zb.entity.ZjWorkDispatchEngineer;
import com.zb.entity.ZjWorkDispatchEngineerCooperate;
import com.zb.extend.NetworkEmpDispatchOrderListExtend;
import com.zb.mapper.ZjWorkDispatchEngineerMapper;
import com.zb.mobile.NetworkMobileOrderListSelectVo;
import com.zb.mobile.NetworkMobileOrderMessageListSelectVo;
import com.zb.mobile.NetworkOrderStatusQtyVo;
import com.zb.service.ZjWorkDispatchEngineerCooperateService;
import com.zb.service.ZjWorkDispatchEngineerService;
import com.zb.utils.ZbDateConvertUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 * 工单分派工程师表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-23
 */
@Service
public class ZjWorkDispatchEngineerServiceImpl extends ServiceImpl<ZjWorkDispatchEngineerMapper, ZjWorkDispatchEngineer> implements ZjWorkDispatchEngineerService {
    @Resource
    private ZjWorkDispatchEngineerMapper workDispatchEngineerMapper;

    // 保存工程师确认接单
    @Override
    public void saveEngineerSureReceiveOrder(Long id) {
        ZjWorkDispatchEngineer newDispatchEngineer = new ZjWorkDispatchEngineer();
        newDispatchEngineer.setId(id);
        newDispatchEngineer.setStatus(WorkConstant.DISPATCH_ENGINEER_STATUS_2);
        newDispatchEngineer.setReceivingOrderTime(ZbDateConvertUtil.dateToDateStr(new Date()));
        this.updateById(newDispatchEngineer);
    }

    // 保存工程师拒绝接单
    @Override
    public void saveEngineerRefuseReceiveOrder(Long id, String remark) {
        ZjWorkDispatchEngineer engineer = new ZjWorkDispatchEngineer();
        engineer.setId(id);
        engineer.setStatus(WorkConstant.DISPATCH_ENGINEER_STATUS_6);
        engineer.setRemark(remark);
        this.updateById(engineer);
    }

    // 根据服务人员ID获取未确认接单数量
    @Override
    public long getWaitReceivingOrderQty(Long networkEmpId) {
        return workDispatchEngineerMapper.getWaitReceivingOrderQty(networkEmpId);
    }

    // 根据服务人员ID获取未签到的工单数量
    @Override
    public long getWaitSignInOrderQty(Long custAccId, Long networkId, Long networkEmpId) {
        return workDispatchEngineerMapper.getWaitSignInOrderQty(custAccId, networkId, networkEmpId);
    }

    // 分页获取服务人员工单
    @Override
    public List<Long> getWorkIdByNetworkIdAndNetworkEmpId(Long networkId, Long networkEmpId, NetworkMobileOrderMessageListSelectVo selectVo) {
        QueryWrapper<ZjWorkDispatchEngineer> wrapper = new QueryWrapper<>();
        wrapper.select("work_id");
        wrapper.eq("network_emp_id", networkEmpId);
        if (null != networkId && networkId > 0) {
            wrapper.eq("network_id", networkId);
        }
        if (null != selectVo.getStatus()) {
            wrapper.eq("status", selectVo.getStatus());
        }
        return this.list(wrapper).stream().map(ZjWorkDispatchEngineer::getWorkId).toList();
    }

    // 根据服务商人员ID & 工单ID获取派工信息
    @Override
    public List<ZjWorkDispatchEngineer> getByNetworkEmpIdAndWorkIds(Long networkId, Long networkEmpId, List<Long> workIds) {
        if (CollectionUtil.isEmpty(workIds) || null == networkEmpId || networkEmpId == 0) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkDispatchEngineer> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_id", "network_id", "network_emp_id", "status", "dispatch_time", "receiving_order_time", "is_reservation AS reservation",
                "reservation_date", "reservation_start_time", "reservation_end_time", "is_sign_in aS signIn");
        wrapper.eq("network_emp_id", networkEmpId)
                .in("work_id", workIds)
                .between("status", WorkConstant.DISPATCH_ENGINEER_STATUS_1, WorkConstant.DISPATCH_ENGINEER_STATUS_5);
        if (null != networkId) {
            wrapper.eq("network_id", networkId);
        }
        return this.list(wrapper);
    }

    // 根据工单ID&服务商ID&服务商人员ID获取派工信息
    @Override
    public ZjWorkDispatchEngineer getByWorkIdAndNetworkIdAndNetworkEmpId(Long workId, Long networkId, Long networkEmpId) {
        QueryWrapper<ZjWorkDispatchEngineer> wrapper = new QueryWrapper<>();
        wrapper.eq("work_id", workId).eq("network_emp_id", networkEmpId)
                .between("status", WorkConstant.DISPATCH_ENGINEER_STATUS_1, WorkConstant.DISPATCH_ENGINEER_STATUS_4);
        if (null != networkId && networkId > 0) {
            wrapper.eq("network_id", networkId);
        }
        return this.getOne(wrapper);
    }

    // 获取服务商工单列表状态数量
    @Override
    public NetworkOrderStatusQtyVo getNetworkOrderListStatusQty(Long networkId, Long networkEmpId, String keyword) {
        NetworkOrderStatusQtyVo networkOrderListStatusQty = workDispatchEngineerMapper
                .getNetworkOrderListStatusQty(networkId, networkEmpId, keyword);
        // 新增待核销
        Long isHxTotal = networkOrderListStatusQty.getServiceStatusQty() - networkOrderListStatusQty.getWaitSignQty();
        Long isPauseTotal = workDispatchEngineerMapper.getIsPauseTotal();
        networkOrderListStatusQty.setHxWaitQty(isHxTotal);
        networkOrderListStatusQty.setIsPauseQty(isPauseTotal);
        long waitCheckTotal = networkOrderListStatusQty.getFeeWaitCheckQty() + networkOrderListStatusQty.getSpareWaitCheckQty() +
                networkOrderListStatusQty.getTransferOrderWaitCheckQty() + networkOrderListStatusQty.getCancelOrderWaitCheckQty() +
                networkOrderListStatusQty.getChangeOrderWaitCheckQty();
        networkOrderListStatusQty.setWaitCheckQty(waitCheckTotal);
        return networkOrderListStatusQty;
    }

    // 获取工程师工单列表
    @Override
    public List<NetworkEmpDispatchOrderListExtend> getNetworkEmpOrderList(Long custAccId, Long networkId, Long networkEmpId, NetworkMobileOrderListSelectVo selectVo) {
        return workDispatchEngineerMapper.getNetworkEmpOrderList(custAccId, networkId, networkEmpId, selectVo);
    }

    // 获取工程师工单列表总记录数
    @Override
    public long getNetworkOrderListCount(Long custAccId, Long networkId, Long networkEmpId, NetworkMobileOrderListSelectVo selectVo) {
        return workDispatchEngineerMapper.getNetworkOrderListCount(custAccId, networkId, networkEmpId, selectVo);
    }

    // 根据工单ID获取工单分配工程师信息
    @Override
    public List<ZjWorkDispatchEngineer> getByWorkIds(List<Long> workIds) {
        if (CollectionUtil.isEmpty(workIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkDispatchEngineer> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_id", "status", "dispatch_time", "receiving_order_time", "reservation_date", "call_phone_qty", "is_sign_in AS signIn", "gmt_update")
                .between("status", WorkConstant.DISPATCH_ENGINEER_STATUS_1, WorkConstant.DISPATCH_ENGINEER_STATUS_2).in("work_id", workIds);
        return this.list(wrapper);
    }

    // 根据服务人员ID&工单ID获取服务人员信息
    @Override
    public ZjWorkDispatchEngineer getByNetworkEmpIdAndWorkId(Long networkEmpId, Long workId) {
        QueryWrapper<ZjWorkDispatchEngineer> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_id", "network_emp_id", "status", "dispatch_time", "receiving_order_time", "is_reservation AS reservation", "reservation_date",
                        "reservation_start_time", "call_phone_qty", "is_sign_in AS signIn", "gmt_update")
                .between("status", WorkConstant.DISPATCH_ENGINEER_STATUS_1, WorkConstant.DISPATCH_ENGINEER_STATUS_5)
                .eq("work_id", workId).eq("network_emp_id", networkEmpId);
        return this.getOne(wrapper);
    }

}
